package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MovementManager;
import com.itheima.domain.mongo.Movement;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    private MovementManager movementManager;

    //发布动态
    @PostMapping(value = "/movements",name = "0201") //前台过来的图片是一个数组
    public void saveMovement(Movement movement,MultipartFile[] imageContent) throws IOException {
        movementManager.saveMovement(movement,imageContent);
    }


    //查询我的动态
    @GetMapping(value = "/movements/all",name = "0202")
    public PageBeanVo findMyMovement(Long userId,
                                     @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize)
    {
            return movementManager.findMyMovement(userId,pageNum,pageSize);
    }

    //查询好友动态
    @GetMapping(value = "/movements")
    public PageBeanVo findFriendMovement(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize)
    {
        return movementManager.findFriendMovement(pageNum, pageSize);
    }

    //查询推荐给我的动态
    @GetMapping(value = "/movements/recommend")
    public PageBeanVo findRecommendMovement(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize)
    {

        return movementManager.findRecommendMovement(pageNum,pageSize);
    }

    //动态点赞
    @GetMapping(value = "/movements/{id}/like",name = "0203")
    public Integer saveMovementLike(@PathVariable("id") String movementId)
    {
            return movementManager.saveMovementLike(movementId);
    }

    //取消点赞
    @GetMapping(value = "/movements/{id}/dislike",name = "0206")
    public Integer deleteMovementLike(@PathVariable("id") String movementId)
    {
        return movementManager.deleteMovementLike(movementId);
    }

    //动态喜欢
    @GetMapping(value = "/movements/{id}/love",name = "0204")
    public Integer saveMovementLove(@PathVariable("id") String movementId) {
        return movementManager.saveMovementLove(movementId);
    }

    //取消喜欢
    @GetMapping(value = "/movements/{id}/unlove",name = "0207")
    public Integer deleteMovementLove(@PathVariable("id") String movementId) {
        return movementManager.deleteMovementLove(movementId);
    }

    //根据动态id查询动态详情
    @GetMapping(value = "/movements/{id}",name = "0202")
    public MovementVo findMovementById(@PathVariable("id") String movementId){
        return movementManager.findMovementById(movementId);
    }

    //查询动态评论区列表
    @GetMapping("/comments")
    public PageBeanVo findMovementCommentList(String movementId,
                                              @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize)
    {
        return movementManager.findMovementCommentList(movementId,pageNum,pageSize);
    }

    //保存评论
    @PostMapping(value = "/comments",name = "0205")
    public void saveMovementComment(@RequestBody Map<String,String> map){
        //1. 接收参数
        String movementId = map.get("movementId");
        String commentContent = map.get("comment");

        //2. 调用service保存
        movementManager.saveMovementComment(movementId,commentContent);
    }

}
